home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / progut~1 / iostream.zoo / include / editbuf.h next >
Encoding:
C/C++ Source or Header  |  1991-09-22  |  6.3 KB  |  168 lines

  1. //    This is part of the iostream library, providing input/output for C++.
  2. //    Copyright (C) 1991 Per Bothner.
  3. //
  4. //    This library is free software; you can redistribute it and/or
  5. //    modify it under the terms of the GNU Library General Public
  6. //    License as published by the Free Software Foundation; either
  7. //    version 2 of the License, or (at your option) any later version.
  8. //
  9. //    This library is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. //    Library General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU Library General Public
  15. //    License along with this library; if not, write to the Free
  16. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #ifndef _EDITBUF_H
  19. #define _EDITBUF_H
  20. #pragma interface
  21. #include <stdio.h>
  22. #include <fstream.h>
  23.  
  24. typedef unsigned long mark_pointer;
  25. // At some point, it might be nice to parameterize this code
  26. // in terms of buf_char.
  27. typedef /*unsigned*/ char buf_char;
  28.  
  29. // Logical pos from start of buffer (does not count gap).
  30. typedef long buf_index;
  31.             
  32. // Pos from start of buffer, possibly including gap_size.
  33. typedef long buf_offset; 
  34.  
  35. #if 0
  36. struct buf_cookie {
  37.     FILE *file;
  38.     struct edit_string *str;
  39.     struct buf_cookie *next;
  40.     buf_index tell();
  41. };
  42. #endif
  43.  
  44. // A edit_string is defined as the region between the 'start' and 'end' marks.
  45. // Normally (always?) 'start->insert_before()' should be false,
  46. // and 'end->insert_before()' should be true.
  47.  
  48. struct edit_string {
  49.     struct edit_buffer *buffer; // buffer that 'start' and 'end' belong to
  50.     struct edit_mark *start, *end;
  51.     long length() const; // count of buf_chars currently in string
  52.     inline edit_string(struct edit_buffer *b,
  53.               struct edit_mark *ms, struct edit_mark *me)
  54.     { buffer = b; start = ms; end = me; }
  55. /* Make a fresh, contiguous copy of the data in STR.
  56.    Assign length of STR to *LENP.
  57.    (Output has extra NUL at out[*LENP].) */
  58.     buf_char *copy_bytes(size_t *lenp) const;
  59. //    FILE *open_file(char *mode);
  60.     void assign(struct edit_string *src); // copy bytes from src to this
  61. };
  62.  
  63. struct edit_streambuf : public streambuf {
  64.     friend edit_buffer;
  65.     edit_string *str;
  66.     edit_streambuf* next; // Chain of edit_streambuf's for a edit_buffer.
  67.     short _mode;
  68.     edit_streambuf(edit_string* bstr, int mode);
  69.     ~edit_streambuf();
  70.     virtual int underflow();
  71.     virtual int overflow(int c = EOF);
  72.     virtual streamoff seekoff(streamoff, seek_dir, int mode=ios::in|ios::out);
  73.     void flush_to_buffer();
  74.     void flush_to_buffer(edit_buffer* buffer);
  75.     int _inserting;
  76.     int inserting() { return _inserting; }
  77.     void inserting(int i) { _inserting = i; }
  78. //    int delete_chars(int count, char* cut_buf); Not implemented.
  79.     int truncate();
  80.     int is_reading() { return gptr() != NULL; }
  81.     buf_char* current() { return is_reading() ? gptr() : pptr(); }
  82.   protected:
  83.     void edit_streambuf::disconnect_gap_from_file(edit_buffer* buffer);
  84. };
  85.  
  86. // A 'edit_mark' indicates a position in a buffer.
  87. // It is "attached" the text (rather than the offset).
  88. // There are two kinds of mark, which have different behavior
  89. // when text is inserted at the mark:
  90. // If 'insert_before()' is true the mark will be adjusted to be
  91. // *after* the new text.
  92.  
  93. struct edit_mark {
  94.     struct edit_mark *chain;
  95.     mark_pointer _pos;
  96.     inline int insert_before() { return _pos & 1; }
  97.     inline unsigned long index_in_buffer(struct edit_buffer *buffer)
  98.     { return _pos >> 1; }
  99.     inline buf_char *ptr(struct edit_buffer *buf);
  100.     buf_index tell();
  101.     struct edit_mark *new_mark(struct edit_string *str, long delta);
  102.     edit_buffer *buffer();
  103.     ~edit_mark();
  104. };
  105.  
  106. // A 'edit_buffer' consists of a sequence of buf_chars (the data),
  107. // a list of edit_marks pointing into the data, and a list of FILEs
  108. // also pointing into the data.
  109. // A 'edit_buffer' coerced to a edit_string is the string of
  110. // all the buf_chars in the buffer.
  111.  
  112. // This implementation uses a conventional buffer gap (as in Emacs).
  113. // The gap start is defined by de-referencing a (buf_char**).
  114. // This is because sometimes a FILE is inserting into the buffer,
  115. // so rather than having each putc adjust the gap, we use indirection
  116. // to have the gap be defined as the write pointer of the FILE.
  117. // (This assumes that putc adjusts a pointer (as in GNU's libc), not an index.)
  118.  
  119. struct edit_buffer {
  120.     buf_char *data; /* == emacs buffer_text.p1+1 */
  121.     buf_char *_gap_start;
  122.     edit_streambuf* _writer; // If non-NULL, currently writing stream
  123.     inline buf_char *gap_start()
  124.     { return _writer ? _writer->pptr() : _gap_start; }
  125.     buf_offset __gap_end_pos; // size of part 1 + size of gap
  126.     /* int gap; implicit: buf_size - size1 - size2 */
  127.     int buf_size;
  128.     struct edit_streambuf *files;
  129.     struct edit_mark start_mark;
  130.     struct edit_mark end_mark;
  131.     edit_buffer();
  132.     inline buf_offset gap_end_pos() { return __gap_end_pos; }
  133.     inline struct edit_mark *start_marker() { return &start_mark; }
  134.     inline struct edit_mark *end_marker() { return &end_mark; }
  135. /* these should be protected, ultimately */
  136.     buf_index tell(edit_mark*);
  137.     buf_index tell(buf_char*);
  138.     inline buf_char *gap_end() { return data + gap_end_pos(); }
  139.     inline int gap_size() { return gap_end() - gap_start(); }
  140.     inline int size1() { return gap_start() - data; }
  141.     inline int size2() { return buf_size - gap_end_pos(); }
  142.     inline struct edit_mark * mark_list() { return &start_mark; }
  143.     void make_gap (buf_offset);
  144.     void move_gap (buf_offset pos);
  145.     void move_gap (buf_char *pos) { move_gap(pos - data); }
  146.     void gap_left (int pos);
  147.     void gap_right (int pos);
  148.     void adjust_markers(mark_pointer low, mark_pointer high,
  149.             int amount, buf_char *old_data);
  150.     void delete_range(buf_index from, buf_index to);
  151.     void delete_range(struct edit_mark *start, struct edit_mark *end);
  152. };
  153.  
  154. extern buf_char * bstr_copy(struct edit_string *str, int *lenp);
  155.  
  156. // Convert a edit_mark to a (buf_char*)
  157.  
  158. inline buf_char *edit_mark::ptr(struct edit_buffer *buf)
  159.     { return buf->data + index_in_buffer(buf); }
  160.  
  161. inline void edit_streambuf::flush_to_buffer()
  162. {
  163.     edit_buffer* buffer = str->buffer;
  164.     if (buffer->_writer == this) flush_to_buffer(buffer);
  165. }
  166. #endif /* !_EDITBUF_H*/
  167.  
  168.